package cn.hj.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @author HP
 */

public class SpringConfig  implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);



        // DispatcherServlet servlet=new DispatcherServlet(ac);
        //ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        // registration.setInitParameter("contextClass", AnnotationConfigWebApplicationContext.class.getName());
        //registration.setInitParameter("contextConfigLocation",SpringMvcConfig.class.getName());
        //registration.setLoadOnStartup(1);
        //registration.addMapping("/");



        servletContext.addListener(new ContextLoaderListener(ac));
        AnnotationConfigWebApplicationContext mvc=new AnnotationConfigWebApplicationContext();
        mvc.register(SpringMvcConfig.class);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", new DispatcherServlet(mvc));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");

    }
}
